Reduce allocated size of resulting string to really needed (alligned) portion. Reorde...
authoroliskoli <oliskoli>
Sun, 3 Dec 2006 21:26:58 +0000 (21:26 +0000)
committeroliskoli <oliskoli>
Sun, 3 Dec 2006 21:26:58 +0000 (21:26 +0000)
util.c

diff --git a/util.c b/util.c
index 1849e9bd8a96b18677f06632642f549d542abc57..1fabfe0c05b86ae78812c0d6bc6f90dbea91f6bf 100644 (file)
--- a/util.c
+++ b/util.c
@@ -298,9 +298,13 @@ xfputs(const char *errtxt, const char *s, FILE *stream)
 int
 xasprintf(char **strp, const char *fmt, ...)
 {
-       /* From http://perfec.to/vsnprintf/pasprintf.c */
+/* From http://perfec.to/vsnprintf/pasprintf.c */
 /* size of first buffer malloc; start small to exercise grow routines */
-#define        FIRSTSIZE       64
+#ifdef DEBUG_MEM
+# define       FIRSTSIZE       64
+#else
+# define       FIRSTSIZE       1
+#endif
        char *buf = NULL;
        int bufsize;
        char *newbuf;
@@ -365,6 +369,14 @@ xasprintf(char **strp, const char *fmt, ...)
                        break;
                }
        }
+       /* Prevent us from allocating millions of unused bytes. */
+       /* O.K.: I think this is not the final solution. */
+       if (bufsize > outsize + 1) {
+               const unsigned ptrsz = sizeof(buf);
+               if (((bufsize + ptrsz + 1) / ptrsz) > ((outsize + ptrsz + 1) / ptrsz))
+                       buf = xrealloc(buf, outsize + 1);       
+
+       }
        *strp = buf;
        return outsize;
 }
@@ -940,30 +952,31 @@ endian_write_float(void* ptr, float f, int write_le)
   }
 }
 
-double
-pdb_read_double( void *ptr ) {return endian_read_double(ptr, 0);}
-
 float
-pdb_read_float( void *ptr ) {return endian_read_float(ptr, 0);}
+le_read_float( void *ptr ) {return endian_read_float(ptr, 1);}
 
-double 
-le_read_double( void *ptr ) {return endian_read_double(ptr,1);}
+void
+le_write_float( void *ptr, float f ) {endian_write_float(ptr,f,1);}
 
-double 
-be_read_double( void *ptr ) {return endian_read_double(ptr,0);}
+float
+be_read_float( void *ptr ) {return endian_read_float(ptr, 0);}
 
 void
-pdb_write_double( void *ptr, double d ) {endian_write_double(ptr,d,0);}
+be_write_float( void *ptr, float f ) {endian_write_float(ptr,f,0);}
 
-void
-pdb_write_float( void *ptr, float f ) {endian_write_float(ptr,f,0);}
+double 
+le_read_double( void *ptr ) {return endian_read_double(ptr,1);}
 
 void
 le_write_double( void *ptr, double d ) {endian_write_double(ptr,d,1);}
 
-void 
+double 
+be_read_double( void *ptr ) {return endian_read_double(ptr,0);}
+
+void
 be_write_double( void *ptr, double d ) {endian_write_double(ptr,d,0);}
 
+
 /* Magellan and PCX formats use this DDMM.mm format */
 double ddmm2degrees(double pcx_val) {
        double minutes;